﻿using movie.tool.library.binding;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace movie.tool.module.film.model
{
    /// <summary>
    /// 剧本配置项目
    /// </summary>
    public class MovieProject : NotifyPropertyChanged
    {
        private long id;
        public long Id
        {
            get => id;
            set
            {
                id = value;
                SendPropertyChanged(nameof(Id));
            }
        }
        private MovieConfig movie;
        public MovieConfig Movie
        {
            get => movie;
            set
            {
                movie = value;
                SendPropertyChanged(nameof(Movie));
            }
        }
        private ObservableCollection<SceneConfig> scenes;
        public ObservableCollection<SceneConfig> Scenes
        {
            get => scenes;
            set
            {
                scenes = value;
                SendPropertyChanged(nameof(Scenes));
            }
        }
        private ObservableCollection<ClueCardGroupConfig> clueCardGroups;
        public ObservableCollection<ClueCardGroupConfig> ClueCardGroups
        {
            get => clueCardGroups;
            set
            {
                clueCardGroups = value;
                SendPropertyChanged(nameof(ClueCardGroups));
            }
        }
        private ObservableCollection<PlayerRoleConfig> roles;
        public ObservableCollection<PlayerRoleConfig> Roles
        {
            get => roles;
            set
            {
                roles = value;
                SendPropertyChanged(nameof(Roles));
            }
        }
        private ObservableCollection<BookChapter> chapters;
        public ObservableCollection<BookChapter> Chapters
        {
            get => chapters;
            set
            {
                chapters = value;
                SendPropertyChanged(nameof(Chapters));
            }
        }
        private ObservableCollection<PlayRoleBookConfig> books;
        public ObservableCollection<PlayRoleBookConfig> Books
        {
            get => books;
            set
            {
                books = value;
                SendPropertyChanged(nameof(Books));
            }
        }
        public bool ImportProject { get; set; }

        public BookChapter FindChapter(int sequence)
        {
            if (Chapters == null || Chapters.Count <= 0)
            {
                return null;
            }
            foreach (BookChapter chapter in Chapters)
            {
                if (chapter.Sequence == sequence)
                {
                    return chapter;
                }
            }
            return null;
        }
        /// <summary>
        /// 根据名称查找章节对象
        /// </summary>
        /// <param name="name">章节名称</param>
        /// <returns></returns>
        public BookChapter FindChapter(string name)
        {
            if (Chapters == null || Chapters.Count <= 0)
            {
                return null;
            }
            foreach (BookChapter chapter in Chapters)
            {
                if (Equals(chapter.Name, name))
                {
                    return chapter;
                }
            }
            return null;
        }

        /// <summary>
        /// 根据id查找章节对象
        /// </summary>
        /// <param name="name">章节名称</param>
        /// <returns></returns>
        public BookChapter FindChapter(long id)
        {
            if (Chapters == null || Chapters.Count <= 0)
            {
                return null;
            }
            foreach (BookChapter chapter in Chapters)
            {
                if (chapter.Id == id)
                {
                    return chapter;
                }
            }
            return null;
        }

        public PlayRoleBookConfig FindBook(long playRoleId,long chapterId)
        {
            if(Books == null || Books.Count <= 0)
            {
                return null;
            }
            foreach(PlayRoleBookConfig b in Books)
            {
                if(b.PlayRoleId == playRoleId && b.ChapterId == chapterId)
                {
                    return b;
                }
            }
            return null;
        }

        public PlayRoleBookConfig FindBook(string roleName, string chapterName)
        {
            if (Books == null || Books.Count <= 0)
            {
                return null;
            }
            foreach (PlayRoleBookConfig b in Books)
            {
                if (Equals(b.Name,chapterName) && Equals(b.RoleName, roleName))
                {
                    return b;
                }
            }
            return null;
        }

        public SceneConfig FindScene(int sequence)
        {
            if(Scenes == null || Scenes.Count <= 0)
            {
                return null;
            }
            foreach(SceneConfig sc in Scenes) {
                if(sc.Sequence == sequence)
                {
                    return sc;
                }
            }
            return null;
        }

        public SceneConfig FindScene(string name)
        {
            if (Scenes == null || Scenes.Count <= 0)
            {
                return null;
            }
            foreach (SceneConfig sc in Scenes)
            {
                if (Equals(sc.Name,name))
                {
                    return sc;
                }
            }
            return null;
        }
        public void ResortScene()
        {
            if (Scenes == null || Scenes.Count == 0)
            {
                return;
            }
            List<SceneConfig> list = Scenes.ToList();
            list.Sort((item1, item2) => {
                if (item1 == item2)
                {
                    return 0;
                }
                if (item1 == null)
                {
                    return -1;
                }
                if (item2 == null)
                {
                    return 1;
                }
                return item1.Sequence - item2.Sequence;
            });
            Scenes = new ObservableCollection<SceneConfig>(list);
        }

        public void ResortChapter()
        {
            if (Chapters == null || Chapters.Count == 0)
            {
                return;
            }
            List<BookChapter> list = Chapters.ToList();
            list.Sort((item1, item2) => {
                if (item1 == item2)
                {
                    return 0;
                }
                if (item1 == null)
                {
                    return -1;
                }
                if (item2 == null)
                {
                    return 1;
                }
                return item1.Sequence - item2.Sequence;
            });
            Chapters = new ObservableCollection<BookChapter>(list);
        }

        public ClueCardGroupConfig FindGroup(string name)
        {
            if (ClueCardGroups == null || ClueCardGroups.Count <= 0)
            {
                return null;
            }
            foreach (ClueCardGroupConfig ccg in ClueCardGroups)
            {
                if (Equals(ccg.Name, name))
                {
                    return ccg;
                }
            }
            return null;
        }

        public PlayerRoleConfig FindRole(string name)
        {
            if (Roles == null || Roles.Count <= 0)
            {
                return null;
            }
            foreach (PlayerRoleConfig role in Roles)
            {
                if (Equals(role.Name, name))
                {
                    return role;
                }
            }
            return null;
        }
        public PlayerRoleConfig FindRole(long id)
        {
            if (Roles == null || Roles.Count <= 0)
            {
                return null;
            }
            foreach (PlayerRoleConfig role in Roles)
            {
                if (role.Id == id)
                {
                    return role;
                }
            }
            return null;
        }
    }
}
